﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Data.Repository;
using YiSha.Entity.OrganizationManage;
using YiSha.Entity.SystemManage;
using YiSha.Model;
using YiSha.Model.Param.SystemManage;
using YiSha.Service.OrganizationManage;
using YiSha.Service.SystemManage;
using YiSha.Util.Model;
using YiSha.Util;
using YiSha.Enums;

namespace YiSha.Service.SystemManage
{
    public class LogOperateService : RepositoryFactory
    {

        #region 获取数据
        public async Task<TData<List<LogOperateEntity>>> GetList(LogOperateListParam param)
        {
            var obj = new TData<List<LogOperateEntity>>();
            var strSql = new StringBuilder();
            var filter = ListFilter(param, strSql);
            var list = SqlSugarAdapters.GetList<LogOperateEntity>(strSql.ToString(), filter);
            obj.Data = list;
            return obj;
        }

        public async Task<TData<List<LogOperateEntity>>> GetPageList(LogOperateListParam param, Pagination pagination)
        {
            var obj = new TData<List<LogOperateEntity>>();
            var strSql = new StringBuilder();
            var filter = ListFilter(param, strSql);
            var list = SqlSugarAdapters.GetPager<LogOperateEntity>(strSql.ToString(), filter, pagination);
            obj.Data = list.Item2;
            obj.Total = list.Item1;
            return obj;
        }

        public async Task<TData<LogOperateEntity>> GetEntity(string id)
        {
            var sql = $@"SELECT  a.Id,
                                    a.BaseCreateTime,
                                    a.BaseCreatorId,
                                    a.LogStatus,
                                    a.IpAddress,
                                    a.IpLocation,
                                    a.Remark,
                                    a.ExecuteUrl,
                                    a.ExecuteParam,
                                    a.ExecuteResult,
                                    a.ExecuteTime,
                                    b.UserName,
                                    c.DepartmentName
                            FROM    SysLogOperate a
                                    LEFT JOIN SysUser b ON a.BaseCreatorId = b.Id
                                    LEFT JOIN SysDepartment c ON b.DepartmentId = c.Id
                            WHERE   1 = 1 and a.Id=@Id";
            var info = SqlSugarAdapters.SqlQuery<LogOperateEntity>(sql, new { Id = id }).FirstOrDefault();

            return new TData<LogOperateEntity> { Data = info };
        }
        #endregion

        #region 提交数据
        public async Task<TData> SaveForm(LogOperateEntity entity)
        {
            var obj = new TData();
            try
            {
                if (entity.Id.IsEmpty())
                {
                    await entity.Create();
                    SqlSugarAdapters.Insert(entity);
                }
                else
                {
                    SqlSugarAdapters.Update(entity);
                }
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }

        public async Task<TData> DeleteForm(List<string> ids)
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.Delete<LogOperateEntity>(s => ids.Contains(s.Id));
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }

        public async Task<TData> RemoveAllForm()
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.ExecuteBySql("truncate table SysLogOperate");
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        #endregion

        #region 私有方法
        private Dictionary<string, object> ListFilter(LogOperateListParam param, StringBuilder strSql)
        {
            strSql.Append(@"SELECT  a.Id,
                                    a.BaseCreateTime,
                                    a.BaseCreatorId,
                                    a.LogStatus,
                                    a.IpAddress,
                                    a.IpLocation,
                                    a.Remark,
                                    a.ExecuteUrl,
                                    a.ExecuteParam,
                                    a.ExecuteResult,
                                    a.ExecuteTime,
                                    b.UserName,
                                    c.DepartmentName
                            FROM    SysLogOperate a
                                    LEFT JOIN SysUser b ON a.BaseCreatorId = b.Id
                                    LEFT JOIN SysDepartment c ON b.DepartmentId = c.Id
                            WHERE   1 = 1");
            var parameter = new Dictionary<string, object>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.UserName))
                {
                    strSql.Append(" AND b.UserName like @UserName");
                    parameter.Add("UserName", '%' + param.UserName + '%');
                }
                if (param.LogStatus > -1)
                {
                    strSql.Append(" AND a.LogStatus = @LogStatus");
                    parameter.Add("LogStatus", param.LogStatus);
                }
                if (!string.IsNullOrEmpty(param.ExecuteUrl))
                {
                    strSql.Append(" AND a.ExecuteUrl like @ExecuteUrl");
                    parameter.Add("ExecuteUrl", '%' + param.ExecuteUrl + '%');
                }
                if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
                {
                    strSql.Append(" AND a.BaseCreateTime >= @StartTime");
                    parameter.Add("StartTime", param.StartTime);
                }
                if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
                {
                    param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    strSql.Append(" AND a.BaseCreateTime <= @EndTime");
                    parameter.Add("EndTime", param.EndTime);
                }
            }
            return parameter;
        }
        #endregion
    }
}
